#include "sysv_msg.h"

#define MAX_LEN 1024

int main(int argc,char *argv[])
{
  int mqid,c,oflag,size;
  long type;
  size_t len;
  struct msgbuf *buf;
 
  type = oflag = 0;
  while((c=getopt(argc,argv,"nt:")) !=-1){
    switch(c){
      case 'n':
        oflag |= IPC_NOWAIT; 
        break;
      case 't':
        type = atol(optarg);
        break;
    }
  } 
  
  if(optind != (argc-1))
    err_quit("Usage: msgrcv [-t type] [-n] <pathname> ");
 
  mqid = msgget(ftok(argv[optind],0),MSG_R);
  if(mqid ==-1)
    err_sys("msgget error");
  
  buf = (struct msgbuf*)malloc(MAX_LEN); 
  if(!buf) 
     err_sys("no space");
  
  if((size=msgrcv(mqid,buf,MAX_LEN,type,oflag)) ==-1)
    err_sys("msgrcv error");
  printf("read %d bytes type=%ld \n",size,buf->mtype);
  exit(0);
}
